import { httpClient } from '@/utils/http/axios';
import { ErrorMessageMode } from '#/axios';
import {
  OrganizationDto,
  OrganizationOutputDto,
  OrganizationRoleUserNode,
  OrganizationSetUserDto,
  OrganizationSetRoleDto,
  OrganizationInputDto,
  OrganizationNodeSimple,
  OrganizationNodeDto,
  OrganizationTreeNodeDto,
} from './model';
import { OrganizationTreeNode } from '@/api/identity/model';
import { AjaxResult } from '@/api/model';
import { PageRequest, PageData } from '@/utils/common/types';

/** 管理-组织机构信息枚举 */
enum Api {
  /** 读取组织机构 */
  Read = '/api/admin/organization/read',
  /** 读取组织机构节点 */
  ReadNode = '/api/admin/organization/readNode',
  /** 读取组织机构树数据 */
  ReadTreeNode = '/api/admin/organization/readTreeNode',

  /** 获取组织机构 */
  GetOrganizations = '/api/admin/organization/getOrganizations',
  /** 获取组织机构树数据 */
  GetOrganizationNodes = '/api/admin/organization/getOrganizationNodes',
  /** 获取排除指定编号后的组织机构数数据 */
  GetTreeNodesExclude = '/api/admin/organization/getTreeNodesExclude',

  /** 读取组织机构[用户]树数据 */
  ReadUserOrganizations = '/api/admin/organization/readUserOrganizations',
  /** 读取组织机构[角色]树数据 */
  ReadRoleOrganizations = '/api/admin/organization/readRoleOrganizations',

  /** 新增组织机构 */
  Create = '/api/admin/organization/create',
  /** 更新组织机构 */
  Update = '/api/admin/organization/update',
  /** 删除组织机构 */
  Delete = '/api/admin/organization/delete',

  /** 设置组织机构用户 */
  SetUsers = '/api/admin/organization/setUsers',
  /** 设置组织机构角色 */
  SetRoles = '/api/admin/organization/setRoles',
}

//#region 读取组织机构
/**
 * 读取组织机构
 * @param request 分页请求
 * @param mode 错误消息模型
 */
export function readOrganizations(request: PageRequest, mode: ErrorMessageMode = 'message') {
  return httpClient.post<PageData<OrganizationOutputDto>>(
    {
      url: Api.Read,
      params: request,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion
//#region 读取组织机构节点
/**
 * 读取组织机构节点
 * @param request 分页请求
 * @param mode 错误消息模型
 */
export function readOrganizationNodes(request: PageRequest, mode: ErrorMessageMode = 'message') {
  return httpClient.post<PageData<OrganizationNodeDto>>(
    {
      url: Api.ReadNode,
      params: request,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion
//#region 读取组织机构树数据
/**
 * 读取组织机构树数据
 * @param request 分页请求
 * @param mode 错误消息模型
 */
export function readOrganizationTreeNodes(
  request: PageRequest,
  mode: ErrorMessageMode = 'message',
) {
  return httpClient.post<PageData<OrganizationTreeNodeDto>>(
    {
      url: Api.ReadTreeNode,
      params: request,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion

//#region 获取组织机构
/**
 * 获取组织机构
 * @param organizationIds 组织机构编号
 * @param mode 错误消息模型
 */
export function getOrganizations(
  organizationIds: (string | bigint)[],
  mode: ErrorMessageMode = 'message',
) {
  return httpClient.post<OrganizationDto[]>(
    {
      url: Api.GetOrganizations,
      params: organizationIds,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion
//#region 获取组织机构树数据
/**
 * 获取组织机构树数据
 * @param organizationIds 组织机构编号
 * @param mode 错误消息模型
 */
export function getOrganizationNodes(
  organizationIds: (string | bigint)[],
  mode: ErrorMessageMode = 'message',
) {
  return httpClient.post<OrganizationNodeSimple[]>(
    {
      url: Api.GetOrganizationNodes,
      params: organizationIds,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion
//#region 获取排除指定编号后的组织机构数数据
/**
 * 获取排除指定编号后的组织机构数数据
 * @param organizationId 组织机构编号
 * @param mode 错误消息模型
 */
export function getTreeNodesExclude(
  organizationId: string | bigint,
  mode: ErrorMessageMode = 'message',
) {
  return httpClient.get<OrganizationTreeNode[]>(
    {
      url: Api.GetTreeNodesExclude,
      params: { organizationId: organizationId },
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion

//#region 读取组织机构[用户]树数据
/**
 * 读取组织机构[用户]树数据
 * @param userId 用户编号
 * @param mode 错误消息模型
 */
export function readUserOrganizations(userId: string | bigint, mode: ErrorMessageMode = 'message') {
  return httpClient.get<OrganizationRoleUserNode[]>(
    {
      url: Api.ReadUserOrganizations,
      params: { userId: userId },
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion
//#region 读取组织机构[角色]树数据
/**
 * 读取组织机构[角色]树数据
 * @param roleId 角色编号
 * @param mode 错误消息模型
 */
export function readRoleOrganizations(roleId: string | bigint, mode: ErrorMessageMode = 'message') {
  return httpClient.get<OrganizationRoleUserNode[]>(
    {
      url: Api.ReadRoleOrganizations,
      params: { roleId: roleId },
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion

//#region 新增组织机构
/**
 * 新增组织机构
 * @param dtos 新增组织机构信息
 * @param mode 错误消息模型
 */
export function createOrganizations(
  dtos: OrganizationInputDto[],
  mode: ErrorMessageMode = 'message',
) {
  return httpClient.post<AjaxResult>(
    {
      url: Api.Create,
      params: dtos,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion
//#region 更新组织机构
/**
 * 更新组织机构
 * @param dtos 更新组织机构信息
 * @param mode 错误消息模型
 */
export function updateOrganizations(
  dtos: OrganizationInputDto[],
  mode: ErrorMessageMode = 'message',
) {
  return httpClient.post<AjaxResult>(
    {
      url: Api.Update,
      params: dtos,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion
//#region 删除组织机构
/**
 * 删除组织机构
 * @param ids 要删除的组织机构编号
 * @param mode 错误消息模型
 */
export function deleteOrganizations(ids: (string | bigint)[], mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.Delete,
      params: ids,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion

//#region 设置组织机构用户
/**
 * 设置组织机构用户
 * @param dtos 角色组织机构信息
 * @param mode 错误消息模型
 */
export function setOrganizationUsers(
  dtos: OrganizationSetUserDto,
  mode: ErrorMessageMode = 'message',
) {
  return httpClient.post<AjaxResult>(
    {
      url: Api.SetUsers,
      params: dtos,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion
//#region 设置组织机构角色
/**
 * 设置组织机构角色
 * @param dtos 角色组织机构信息
 * @param mode 错误消息模型
 */
export function setOrganizationRoles(
  dtos: OrganizationSetRoleDto,
  mode: ErrorMessageMode = 'message',
) {
  return httpClient.post<AjaxResult>(
    {
      url: Api.SetRoles,
      params: dtos,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion
